using UnityEngine;

public class PlayerStateBehavior_Rebound : AbstractPlayerStateBehavior
{
	public static PlayerStateBehavior_Rebound instance;

	public static PlayerStateBehavior_Rebound getInstance()
	{
		if (instance == null)
		{
			instance = new PlayerStateBehavior_Rebound();
		}
		return instance;
	}

	public override void enter(Player owner)
	{
		base.enter(owner);
		if (Random.Range(0, 2) == 0)
		{
			owner.playAnimation(AnimationName.player_grabrebound_new, 1f, -1f);
		}
		else
		{
			owner.playAnimation(AnimationName.player_grabrebound_new2, 1f, -1f);
		}
		owner.rebound_is_success = false;
		Player player = null;
		for (int i = 0; i < 5; i++)
		{
			if (owner.getParaTeam().getPlayer(i).isInState(getInstance()))
			{
				player = owner.getParaTeam().getPlayer(i);
				break;
			}
		}
		if (!Instance<Ball>.instance.isGetBall(owner.getPosition()) || !Instance<Ball>.instance.isInState(BallState_Rebound.getInstance()) || Instance<Ball>.instance.isRebounded)
		{
			return;
		}
		if (player == null)
		{
			owner.rebound_is_success = true;
			return;
		}
		owner.rebound_is_success = MathHelper.isInPercent((float)owner.getData_Rebound() / (float)(owner.getData_Rebound() + player.getData_Rebound()) * 100f);
		if (owner.rebound_is_success)
		{
			player.rebound_is_success = false;
		}
		else
		{
			player.rebound_is_success = true;
		}
	}

	public override void fixedUpdate(Player owner)
	{
		base.fixedUpdate(owner);
		if (owner.rebound_is_success && !Instance<Ball>.instance.isGetBall(owner.getPosition()) && !Instance<Ball>.instance.isRebounded)
		{
			owner.rebound_is_success = false;
		}
		Vector3 position = owner.trans_RightHand.position;
		float num = position.y + 0.1f;
		Vector3 position2 = Instance<Ball>.instance.getPosition();
		if (num > position2.y && owner.rebound_is_success && Instance<Ball>.instance.isInState(BallState_Rebound.getInstance()))
		{
			Instance<Ball>.instance.isRebounded = true;
			owner.getBallInHand();
			Instance<Ball>.instance.do_rebound_inhand();
			if (owner.teamType == TeamType.USER)
			{
				Instance<Court>.instance.count_PauseData_User[7]++;
			}
			else
			{
				Instance<Court>.instance.count_PauseData_Com[7]++;
			}
			Instance<AudioController>.instance.play(AudioType.Sound_catch);
		}
		if (owner.rebound_is_success)
		{
			if (!owner.IsPlayingAnimation())
			{
				if (owner.teamType == TeamType.COMPUTER)
				{
					owner.do_dribble_stay();
				}
				else
				{
					Instance<Court>.instance.OnGrabedRebound(owner);
				}
			}
		}
		else if (!owner.IsPlayingAnimation())
		{
			owner.do_idle();
		}
	}
}
